/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.worlds.AssetManager;
import net.worlds.Main;
import net.worlds.Util;

/**
 *
 * @author Jacob
 */
public class LoginFrame extends EStickyFrame{
    private BufferedImage headImage;
    private BufferedImage skinImage;
    private String username;
    public LoginFrame(EComponent parent, int rightPadding)
    {
        super(parent, 200, 24, 0, rightPadding, -1, 0);
    }
    @Override
    public void setVisible(boolean v)
    {
        super.setVisible(v);
        if(v)
        {
            headImage = Util.getGraphicsConfiguration().createCompatibleImage(24, 24);
            
            final int uid = Main.getLoginID();
            username = Main.getLoginName();
            Util.performRequestWithCallback(new Runnable()
            {
                public void run() {
                    try {
                        skinImage = AssetManager.loadImage("http://"+Main.siteURL+"/skins/"+username+".png", "http://"+Main.siteURL+"/skins/"+username+".png");
                    } catch (IOException ex) {
                        try {
                            skinImage = AssetManager.loadImage("http://"+Main.siteURL+"/skins/defaultskin.png", "http://"+Main.siteURL+"/skins/defaultskin.png");
                        } catch (IOException ex1) {
                        }
                    }
                }
            }, new Runnable()
            {
                public void run() {
                    if(skinImage != null)
                    {
                        headImage.createGraphics().drawImage(skinImage, 0, 0, null);
                    }
                }
            });
            Rectangle2D r = EDGESettings.defaultFont.getStringBounds(username, Util.fontRendererContext);
            setWidth((int) r.getWidth()+8+24);
        }
    }
    @Override
    public void render(Graphics2D g)
    {
        if(headImage != null)
        {
            g.drawImage(headImage, getX(), getY(), null);
            g.drawString(username, getX()+24+8, getY()+16);
        }
    }
}
